<?php

include 'includes.php';

$usuario = $_POST['Email'];
$password = md5($_POST['Password']);
$_SESSION['UserIncorrecto'] = null;

$extra = 'index.php';

try {
    $row = R::getAll('SELECT * from usuarios where email = :email', [':email' => $usuario]);

    if ($row[0] != null) {
        if ($password == $row[0]['password']) {
            
            $_SESSION['user'] = $row[0];

            setcookie('user', $row[0]['id'], time() + 60 * 60 * 24 * 7);

        } else {

            $_SESSION['UserIncorrecto'] = "Error";
            $extra = 'login2.php';
        }
    } else {
        $_SESSION['UserIncorrecto'] = "Error";
        $extra = 'login2.php';
    }
} catch (Exception $e) {
    $_SESSION['UserIncorrecto'] = "Error";
}

R::close();

  $host = $_SERVER['HTTP_HOST'];
  $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

  header("Location: http://" . $host . $uri . "/" . $extra);
  die(); 
?>
